using System.Linq;
using MvcContrib.Pagination;
using OTE.Contract;
using OTE.Domain;
using OTE.Web.Infrastructure.Utils;
using OTE.Web.Mvc.ViewModels.Question;
using SharpArch.NHibernate;

namespace OTE.Web.Mvc.Queries.Question
{
    /// <summary>
    /// http://stackoverflow.com/questions/4977038/can-i-use-nhibernate-criteria-to-project-an-entity-and-its-child-collection-onto
    /// </summary>
    public class QuestionQuery : NHibernateQuery, IQuestionQuery
    {
        public IPagination<QuestionViewModel> GetPagedList(int page, int size, int userid, ref int totalPage)
        {
            Domain.Question questionAlias = null;

            var query = Session.QueryOver(() => questionAlias);

            var count = query.ToRowCountQuery();
            var totalCount = count.FutureValue<int>();

            totalPage = Common.GetTotalPage(size, totalCount.Value);

            var firstResult = (page - 1) * size;

            var viewModels = from q in query.List()
                             where q.User.Id == userid
                             select new QuestionViewModel
                                        {
                                            Id = q.Id,
                                            Content = q.Content,
                                            CreatedBy = q.CreatedBy,
                                            CreatedDate = q.CreatedDate,
                                            ModifiedBy = q.ModifiedBy,
                                            ModifiedDate = q.ModifiedDate
                                        };

            viewModels = viewModels.ToList().Skip(firstResult).Take(size).ToArray();

            return new CustomPagination<QuestionViewModel>(viewModels, page, size, totalCount.Value);
        }

        public QuestionDTO GetQuestionById(int id)
        {
            Domain.Question questionAlias = null;

            var questions = Session.QueryOver(() => questionAlias).List();

            var result = from q in questions
                         where q.Id == id
                         select new QuestionDTO
                                    {
                                        Id = q.Id,
                                        CategoryId = q.Category.Id,
                                        Content = q.Content,
                                        CreatedBy = q.CreatedBy,
                                        CreatedDate = q.CreatedDate,
                                        UserId = q.User.Id
                                    };

            return result.FirstOrDefault();
        }

        private QuestionType GetQuestionType(int questionTypeId)
        {
            QuestionType type = QuestionType.Single;
            switch (questionTypeId)
            {
                case (int)QuestionType.Single:
                    type = QuestionType.Single;
                    break;
                case (int)QuestionType.Multi:
                    type = QuestionType.Multi;
                    break;
            }

            return type;
        }
    }
}